﻿using Redbrick.Silverlight.Common;
using System;
using System.ComponentModel;
using Redbrick.Silverlight.Common.Collections;
using System.Windows;

namespace Moria.Common.Model
{
	public class Drawable : MoriaObject, IHasRect
	{
		private Point _pt = new Point();
		private bool _canReposition = true;
		private Rect _rect = new Rect(0, 0, 0, 0);

		protected override bool OnPropertyChanged(PropertyChangedEventArgs args)
		{
			if (args.PropertyName == "X")
			{
				if (this.XPositionUpdated != null)
					this.XPositionUpdated.Invoke(this, new EventArgs());
			}
			if (args.PropertyName == "Y")
			{
				if (this.YPositionUpdated != null)
					this.YPositionUpdated.Invoke(this, new EventArgs());
			}
			return base.OnPropertyChanged(args);
		}

		public bool CanReposition
		{
			get { return this._canReposition; }
			set
			{
				if (value == _canReposition) return;
				_canReposition = value;
				this.RaisePropertyChanged(() => this.CanReposition);
			}
		}

		public double TileWidth
		{
			get { return this._rect.Width; }
			set
			{
				this._rect.Width = value;
				this._rect.X = this._pt.x * this._rect.Width;
				this.RaisePropertyChanged(() => this.Rect);
			}
		}
		public double TileHeight
		{
			get { return this._rect.Height; }
			set
			{
				this._rect.Height = value;
				this._rect.Y = this._pt.y * this._rect.Height;
				this.RaisePropertyChanged(() => this.Rect);
			}
		}

		public int X
		{
			get { return this._pt.x; }
			set
			{
				if (value == _pt.x || !this.CanReposition) return;
				_pt.x = value;
				this.RaisePropertyChanged(() => this.X);
				if (this.TileWidth > 0)
				{
					this._rect.X = _pt.x * this.TileWidth;
					this.RaisePropertyChanged(() => this.Rect);
				}
			}
		}
		public int Y
		{
			get { return this._pt.y; }
			set
			{
				if (value == _pt.y || !CanReposition) return;
				_pt.y = value;
				this.RaisePropertyChanged(() => this.Y);
				if (this.TileHeight > 0)
				{
					this._rect.Y = _pt.y * TileHeight;
					this.RaisePropertyChanged(() => this.Rect);
				}
			}
		}
		public Point Point { get { return this._pt; } }
		public Rect Rect { get { return this._rect; } }
		//public event EventHandler<EventArgs> PositionUpdated;
		public event EventHandler<EventArgs> XPositionUpdated;
		public event EventHandler<EventArgs> YPositionUpdated;

		public virtual string GetDescription() { return string.Empty; }
		public string Description { get { return this.GetDescription(); } }
	}
}
